package day26;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Demo {
    public static void main(String[] args) throws ClassNotFoundException {
        //1.获取class对象  永远不会变
        Class c1 = Person.class;
        Person p = new Person();
        Class c2 = p.getClass();
        Class c3 = Class.forName("day26.Person");

        //2.通过Class对象  获取类中的任意资源
        //c1.getXXX();
        String name = c1.getName();
        System.out.println("全类名:"+name);
        String name2 = c1.getSimpleName();
        System.out.println("类名:"+name2);
        String name3 = c1.getSuperclass().getName();
        System.out.println("父类全类名："+name3);
        System.out.println("----获取属性----");

        //获取类中不包括父类的属性
        Field[] fs= c1.getDeclaredFields();
        for(Field f:fs) {
            System.out.println(f.getName());
            System.out.println(f.getType());
        }
        System.out.println("----获取属性----");
        //获取类中所有的公开属性（包括本类和父类的）
        Field[] fs2= c2.getFields();
        for(Field f:fs2) {
            System.out.println(f.getName());
            System.out.println(f.getType());
        }
        System.out.println("----构造----");
        Constructor[] cons= c2.getDeclaredConstructors();
        for(Constructor c:cons) {
            String methodName = c.getName();
            Class[] cs = c.getParameterTypes();
            System.out.println(methodName);
            System.out.println(Arrays.toString(cs));
        }
        System.out.println("----方法----");
        Method[] ms= c2.getDeclaredMethods();
        for(Method m:ms) {
            System.out.println(m.getName());
            System.out.println(m.getReturnType());
            System.out.println(Arrays.toString(m.getParameterTypes()));
        }
    }
}
